<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" :disabled="mode=='show'" label-width="100px" label-position="left">
			<el-form-item label="指标标题" prop="title">
				<el-input v-model="form.title" placeholder="指标标题" clearable :maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="指标类型" prop="kpiType">
						<sc-select v-model="form.kpiType" :params="{code:'kpiType'}" :apiObj="$API.hr.hrDic.getHrDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<kpiitem v-model:listData="itemJson" :disabled="mode=='show'"></kpiitem>
			<el-form-item label="备注说明" style="margin-top: 20px;">
				<el-input v-model="form.remark" placeholder="备注说明" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import kpiitem from './kpiitem'
export default {
	emits: ['success', 'closed'],
	components: {
		kpiitem,
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '创建KPI考核指标',
				edit: '编辑KPI考核指标',
				show:'KPI考核指标详情'
			},
			templates: [],
			itemJson:[],
			form: {
				itemId: '',
				title: '',
				kpiType: '',
				sortNo: 0,
				remark:'',
				itemJson: '',
			},
			rules: {
				title: [
					{required: true, message: '指标标题不能为空', trigger: 'blur'}
				],
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.itemJson = JSON.stringify(this.itemJson);
					let res;
					if (this.mode == "add") {
						res = await this.$API.hr.hrKpiItem.insertHrKpiItem.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.hr.hrKpiItem.updateHrKpiItem.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.itemId = data.itemId
			this.form.title = data.title
			this.form.kpiType = data.kpiType
			this.form.sortNo = data.sortNo
			this.form.remark = data.remark
			if(data.itemJson!=undefined)
			{
				this.itemJson = JSON.parse(data.itemJson);
			}

		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
